package com.zc.iotDevice.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.zc.iotDevice.domain.ChargeStationPort;
import com.zc.iotDevice.domain.ChargeStationProduct;
import com.zc.iotDevice.mapper.ChargeStationPortMapper;
import com.zc.iotDevice.mapper.ChargeStationProductMapper;
import com.zc.manager.device.domain.IotDevice;
import com.zc.manager.device.entity.DeviceProperty;
import com.zc.manager.device.mapper.IotDeviceMapper;
import com.zc.manager.device.service.impl.IotDeviceServiceImpl;
import com.zc.remoteControl.service.RemoteInstructSendService;
import com.zc.utils.ImageConvertBase64;
import com.zc.utils.QRCodeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.zc.iotDevice.mapper.ChargeStationPileMapper;
import com.zc.iotDevice.domain.ChargeStationPile;
import com.zc.iotDevice.service.ChargeStationPileService;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChargeStationPileServiceImpl implements ChargeStationPileService {

    @Resource
    private ChargeStationPileMapper chargeStationPileMapper;
    @Resource
    private ChargeStationPortMapper chargeStationPortMapper;
    @Resource
    private ChargeStationProductMapper productMapper;
    @Resource
    private IotDeviceMapper deviceMapper;
    @Resource
    private RemoteInstructSendService remoteInstructSendService;

    @Override
    public int deleteByPrimaryKey(String deviceId) {
        return chargeStationPileMapper.deleteByPrimaryKey(deviceId);
    }

    @Override
    public int insert(ChargeStationPile record) {
        ChargeStationPile pile = chargeStationPileMapper.selectByPrimaryKey(record.getDeviceId());
        if (pile != null) {
            throw new RuntimeException("此物联设备已绑定");
        }
        IotDevice iotDevice = deviceMapper.selectById(record.getDeviceId());
        if (iotDevice == null) {
            throw new RuntimeException("未知物联设备");
        }
        ChargeStationProduct chargeStationProduct = productMapper.selectByPrimaryKey(iotDevice.getProductId());
        if (chargeStationProduct == null) {
            throw new RuntimeException("请先创建相关充电桩产品");
        }
        BeanUtils.copyProperties(chargeStationProduct, record);
        //生成二维码
        String content = "https://www.sdzctech.cn:4433/appQrCode?id=" + record.getStationId() + "&code=" + record.getStationCode() + "&deviceId=" + record.getDeviceId();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QRCodeUtil.createCodeToOutputStream(content, outputStream);
        record.setQrCode(("data:image/png;base64," + ImageConvertBase64.bytesEncode2Base64(outputStream.toByteArray())));
        //枪二维码下发
        distributeQRCode(record);
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chargeStationPileMapper.insert(record);
    }

    @Override
    public ChargeStationPile selectByPrimaryKey(String deviceId) {
        return chargeStationPileMapper.selectByPrimaryKey(deviceId);
    }

    @Override
    public int updateByPrimaryKeySelective(ChargeStationPile record) {
        //生成二维码
        String content = "https://www.sdzctech.cn:4433/appQrCode?id=" + record.getStationId() + "&code=" + record.getStationCode() + "&deviceId=" + record.getDeviceId();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QRCodeUtil.createCodeToOutputStream(content, outputStream);
        record.setQrCode(("data:image/png;base64," + ImageConvertBase64.bytesEncode2Base64(outputStream.toByteArray())));
        //枪二维码下发
        distributeQRCode(record);
        return chargeStationPileMapper.updateByPrimaryKeySelective(record);
    }

    //枪二维码下发
    private void distributeQRCode(ChargeStationPile record) {
        //查询枪口
        ChargeStationPort port = new ChargeStationPort();
        port.setDeviceId(record.getDeviceId());
        List<ChargeStationPort> chargeStationPorts = chargeStationPortMapper.selectPortList(port);
        if (chargeStationPorts.size() > 0) {
            for (ChargeStationPort chargeStationPort : chargeStationPorts) {
                //生成二维码
                String gunPos = chargeStationPort.getId();
                String content = "https://www.sdzctech.cn:4433/appQrCode?id=" + record.getStationId() + "&code=" + record.getStationCode() +
                        "&deviceId=" + record.getDeviceId() + "&gunPos=" + gunPos;
//                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//                QRCodeUtil.createCodeToOutputStream(content, outputStream);
//                String QrCode = ("data:image/png;base64," + ImageConvertBase64.bytesEncode2Base64(outputStream.toByteArray()));
                Map<String, Object> stringObjectMap =
                        remoteInstructSendService.distributeQRCode(record.getDeviceId(), gunPos.substring(gunPos.length() - 2), content, "YTSN-CDZ-N01");
                System.out.println("1------>" + record.getDeviceId());
                System.out.println("2------>" + gunPos.substring(gunPos.length() - 2));
                System.out.println("3------>" + content);
                System.out.println("4------>" + "YTSN-CDZ-N01");
            }
        }
    }

    @Override
    public int updateByPrimaryKey(ChargeStationPile record) {
        return chargeStationPileMapper.updateByPrimaryKey(record);
    }

    public List<ChargeStationPile> getList(ChargeStationPile pile) {
        List<ChargeStationPile> list = chargeStationPileMapper.getList(pile);
        /*for (int i = 0; i < list.size(); i++) {
            Map<String, DeviceProperty> AGun = remoteInstructSendService.getRealData(list.get(i).getDeviceId()+"01");
            Map<String, DeviceProperty> BGun = remoteInstructSendService.getRealData(list.get(i).getDeviceId()+"02");
            if(AGun.containsKey("runningState")){
                String AStatic = String.valueOf(AGun.get("runningState").getValue());
                if("3".equals(AStatic)){
                    list.get(i).setServiceStatus("2");
                    continue;
                }
            }
            if(BGun.containsKey("runningState")){
                String BStatic = String.valueOf(BGun.get("runningState").getValue());
                if("3".equals(BStatic)){
                    list.get(i).setServiceStatus("2");
                    continue;
                }
            }
            if("online".equals(list.get(i).getDeviceState())){
                list.get(i).setServiceStatus("1");
            }else {
                list.get(i).setServiceStatus("0");
            }
        }*/
        return list;
    }

    @Override
    public Map<String, Integer> getStatus(BaseEntity entity) {//online 在线 offline  离线 notActive 未激活
        List<ChargeStationPile> list = getList(new ChargeStationPile());
        int total = list.size();
        int onlineNum = 0;
        int offLineNum = 0;
        int notActiveNum = 0;
        for (int i = 0; i < list.size(); i++) {
            if ("online".equals(list.get(i).getDeviceState())) {
                onlineNum = onlineNum + 1;
            } else if ("offline".equals(list.get(i).getDeviceState())) {
                offLineNum = offLineNum + 1;
            } else {
                notActiveNum = notActiveNum + 1;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("total", total);
        map.put("onlineNum", onlineNum);
        map.put("offLineNum", offLineNum);
        map.put("notActiveNum", notActiveNum);
        return map;
    }

    @Override
    public int updateDelState(String id) {
        return chargeStationPileMapper.updateDelState(id);
    }
}
